package com.kclm.config;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

/**
 * @Author : lvhx
 * @Date: 2025/11/5 11:00
 * @Description:
 *SqlSessionFactoryBean 用获取sqlSession对象
 */
@MapperScan("com.kclm.mapper")
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sb  = new SqlSessionFactoryBean();
        sb.setDataSource(dataSource);
        //  给实体类取别名
        sb.setTypeAliasesPackage("com.kclm.pojo");
        //  设置mybatis全局配置文件
//        sb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        // 可选设置  开启驼峰命名映射 Configuration 在mybatis相关的包中
//        Configuration configuration = new Configuration();
//        configuration.setMapUnderscoreToCamelCase(true);
//        sb.setConfiguration(configuration);
        return  sb;
    }
    // MapperScannerConfigurer 用于扫描映射映射文件
//    @Bean
//    public MapperScannerConfigurer  mapperScannerConfigurer(){
//        MapperScannerConfigurer msc  = new MapperScannerConfigurer();
//        msc.setBasePackage("com.kclm.mapper");
//        return msc;
//    }
}
